package dashboard;

import java.util.ArrayList;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

public class CourseRegistry {

	private static CourseRegistry instance = null;
	private List<Course> courses;
	private Objectify ofy = ObjectifyService.begin();

	public CourseRegistry()
	{
		courses = new ArrayList<Course>();
		ObjectifyService.register(Course.class);
		
		Iterator<Course> it = ofy.query(Course.class).fetch().iterator();
		while(it.hasNext())
			courses.add(it.next());
		
	}

	public static CourseRegistry getInstance()
	{
		if(instance == null)
		{
			instance = new CourseRegistry();
		}
		return instance;
	}

	public void addCourse(Course course)
	{
		ofy.put(course);
		courses.add(course);
	}

	public void removeCourse(Course course) 
	{
		ofy.delete(course);
		courses.remove(course);
	}

	public List<Course> getCourses() //TODO Nog doen
	{
		return courses;
	}

	
	public Course getCourseById(long id) 
	{	
		return ofy.query(Course.class).filter("id", id).get();	
	}
	public Course getCourseByName(String name)
	{	
		return ofy.query(Course.class).filter("coursename", name).get();	
	}

	public long getCourseId(String courseName) {
		
		for(Course course: courses){
			if(course.getCourseName().equals(courseName)){
				return course.getId();
			}
		}
		return -1;
	}

	public String getCourseName(long courseId) {
		
	Course course = getCourseById(courseId);
	if(course!=null){
	return(course.getCourseName());
	}
	else{
		return null;
	}
		
	
	}
}
